package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class TouchActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyButton myButton=new MyButton(this);
        myButton.setText("Hello");
        myButton.setTextSize(20);
        myButton.setAllCaps(false);
        setContentView(myButton);
    }

    @SuppressLint("AppCompatCustomView")
    class  MyButton extends Button{

        public MyButton(Context context) {
            super(context);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    Toast.makeText(TouchActivity.this,"按钮按下",Toast.LENGTH_SHORT).show();
                    break;
                    case MotionEvent.ACTION_UP:
                        Toast.makeText(TouchActivity.this,"按钮弹起",Toast.LENGTH_SHORT).show();
                     break;
                     case MotionEvent.ACTION_MOVE:
                         Toast.makeText(TouchActivity.this,"在按钮上移动",Toast.LENGTH_SHORT).show();
                         break;
            }
            return super.onTouchEvent(event);
        }
    }
}


